package com.springlearn.springsourcecode.chap13;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @author wtcode
 * @Description
 * @date 2025/4/2 - 9:39
 */
public class $Proxy0 extends Proxy implements A13.Foo {

    private InvocationHandler h;
    static Method foo;
    static Method bar;

    static {
        try {
            foo = A13.Foo.class.getMethod("foo");
            bar = A13.Foo.class.getMethod("bar");
        } catch (NoSuchMethodException e) {
            throw new NoSuchMethodError(e.getMessage());
        }
    }

    public $Proxy0(InvocationHandler h){
        super(h);
    }


    @Override
    public void foo() {
        //1. 功能增强
        //2. 调用目标
        try {
            Object invoke = h.invoke(this, foo, new Object[0]);
        } catch (RuntimeException | Error e){
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }

    }

    @Override
    public int bar() {
        try {
            int invoke = (int)h.invoke(this, bar, new Object[0]);
            return invoke;
        }catch (RuntimeException | Error e){
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }



}
